package com.xinke.module.system.controller.admin.sms;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.sms.*;
import com.xinke.module.system.model.entity.sms.SmsChannelDO;
import com.xinke.module.system.service.sms.SmsChannelService;

@Tag(name = "管理后台 - 短信渠道")
@RestController
@RequestMapping("/system/sms-channel")
@Validated
public class SmsChannelController {

    @Resource
    private SmsChannelService smsChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建短信渠道")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:create')")
    public Result<Long> createSmsChannel(@Valid @RequestBody SmsChannelSaveReqVO createReqVO) {
        return success(smsChannelService.createSmsChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信渠道")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:update')")
    public Result<Boolean> updateSmsChannel(@Valid @RequestBody SmsChannelSaveReqVO updateReqVO) {
        smsChannelService.updateSmsChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信渠道")
    @Parameter(name = "channelId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:sms-channel:delete')")
    public Result<Boolean> deleteSmsChannel(@RequestParam("channelId") Long channelId) {
        smsChannelService.deleteSmsChannel(channelId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信渠道")
    @Parameter(name = "channelId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:query')")
    public Result<SmsChannelRespVO> getSmsChannel(@RequestParam("channelId") Long channelId) {
        SmsChannelDO smsChannel = smsChannelService.getSmsChannel(channelId);
        return success(BeanUtils.toBean(smsChannel, SmsChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短信渠道分页")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:query')")
    public Result<PageResult<SmsChannelRespVO>> getSmsChannelPage(@Valid SmsChannelPageReqVO pageReqVO) {
        PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SmsChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信渠道 Excel")
    @PreAuthorize("@ss.hasPermission('system:sms-channel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSmsChannelExcel(@Valid SmsChannelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SmsChannelDO> list = smsChannelService.getSmsChannelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信渠道.xls", "数据", SmsChannelRespVO.class,
                        BeanUtils.toBean(list, SmsChannelRespVO.class));
    }

}